package com.shierke.Ume.ui.adapter;


import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import androidx.recyclerview.widget.RecyclerView;

import com.shierke.Ume.R;
import com.shierke.Ume.ui.view.PinnedHeaderAdapter;

import java.util.ArrayList;
import java.util.List;

public class LinearRecyclerAdapter extends PinnedHeaderAdapter<RecyclerView.ViewHolder> {

    private static final int VIEW_TYPE_ITEM_TIME = 0;
    private static final int VIEW_TYPE_ITEM_CONTENT = 1;

    private List<String> mDataList = new ArrayList<>();

    public LinearRecyclerAdapter() {
        this(null);
    }

    public LinearRecyclerAdapter(List<String> dataList) {
        //mDataList = dataList;
        mDataList.addAll(dataList);
    }

    public void setData(List<String> dataList) {
        mDataList = dataList;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_ITEM_TIME) {
            return new TitleHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_linear_title, parent, false));
        } else {
            return new ContentHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_linear_content, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == VIEW_TYPE_ITEM_TIME) {
            TitleHolder titleHolder = (TitleHolder) holder;
           // titleHolder.mTextTitle.setText(mDataList.get(position));
            Log.e("aaaaa",mDataList.get(position));
        } else {
            ContentHolder contentHolder = (ContentHolder) holder;
           // contentHolder.mTextTitle.setText(mDataList.get(position));
        }
    }

    @Override
    public int getItemCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (position % 5 == 0) {
            return VIEW_TYPE_ITEM_TIME;
        } else {
            return VIEW_TYPE_ITEM_CONTENT;
        }
    }

    @Override
    public boolean isPinnedPosition(int position) {
        return getItemViewType(position) == VIEW_TYPE_ITEM_TIME;
    }

    static class ContentHolder extends RecyclerView.ViewHolder {

        TextView mTextTitle;

        ContentHolder(View itemView) {
            super(itemView);
            mTextTitle = itemView.findViewById(R.id.text_index);
        }
    }

    static class TitleHolder extends RecyclerView.ViewHolder {

        TextView mTextTitle;

        TitleHolder(View itemView) {
            super(itemView);
            mTextTitle = itemView.findViewById(R.id.countryName);
        }
    }

}
